package com.app.micaihu.view.newsdetail;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.CommentList1;
import com.app.micaihu.bean.comment.NormalComment;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.CommentUtils1;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.view.comment.SubmitCommentListener;
import com.app.micaihu.view.comment.adapter.NormalCommentAdapter;
import com.app.utils.pulltorefresh.PullToRefreshBase;
import com.app.utils.util.StringUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2016/7/31.
 * 图库详情的评论
 */
public class ImageDetailCommentActivity extends BaseListActivity<NormalComment> {


    private String articleId;
    private int addCommentIndex;//添加评论下标
    private SubmitCommentListener commentListener;
    private boolean hasNormal = true;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            articleId = intent.getStringExtra(Config.Extra.PARAMETER1);
//            articleId = "241922760468812094";//测试用的 评论,1000+的评论数
            if (TextUtils.isEmpty(articleId)) {
                return;
            }
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void changeView() {
        setTitle(getResources().getString(R.string.detail_image_comment_title));
        if (flCommonBottom != null) {
            View view = this.getLayoutInflater().inflate(R.layout.layout_imagecomment_bottom, null);
            flCommonBottom.addView(view);
            flCommonBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showCommentDialog();
                }
            });
        }


    }


    private void showCommentDialog() {
        if (commentListener == null) {
            commentListener = new SubmitCommentListener() {
                @Override
                public void startSubmit() {

                }

                @Override
                public void errorSubmit(VolleyError error) {

                }

                @Override
                public void successSubmit(NormalComment normalComment) {
                    if (dataList != null) {
                        if (dataList.size() == 0) {
                            NormalComment normalCommentTitle = new NormalComment();
                            normalCommentTitle.setCommentTitle(AppApplication.getInstance().getString(R.string.speak_title_new));
                            dataList.add(normalCommentTitle);
                            addCommentIndex = 1;
                        }
                        if (dataList.size() > 0 && hasNormal && addCommentIndex > 1) {
                            NormalComment normalCommentTitle = new NormalComment();
                            normalCommentTitle.setCommentTitle(AppApplication.getInstance().getString(R.string.speak_title_new));
                            dataList.add(normalCommentTitle);
                            hasNormal = false;
                        }
                        try {
                            dataList.add(addCommentIndex, normalComment);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (mAdapter == null) {
                            mAdapter = new NormalCommentAdapter(dataList, mActivity);
                            if (mListView != null) {
                                mListView.setAdapter(mAdapter);
                            }
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            };
        }
        CommentUtils1.getInstance().showDialog(mActivity, mPageName, articleId, null, commentListener, true, false);
    }


    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();
        params.put("articleId", articleId);

        params.put("page", dataPage + "");

        addPostRequest(URIContant.NEWSCOMMENTLIST, new TypeToken<DataBean<CommentList1>>() {
                }.getType(), params,
                new HttpListener<DataBean<CommentList1>>() {
                    @Override
                    public void onStart() {
                        super.onStart();

                        setLoadView(LOADING, null);
                    }

                    @Override
                    public void onError(VolleyError error) {
                        setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
                    }

                    @Override
                    public void onSuccess(DataBean<CommentList1> response) {
                        if (!response.noError()) {
                            setLoadView(DATAERROR, response.getMsg());
                        } else {
                            CommentList1 newsCommentList = response.getData();
                            List<NormalComment> list = null;
                            if (newsCommentList != null) {

                                if (newsCommentList.getHotCmtList() != null && newsCommentList.getHotCmtList().size() > 0) {
                                    for (int i = 0; i < newsCommentList.getHotCmtList().size(); i++) {
                                        if (InteractiveUtils.getInstance().theDefriendIsContains(newsCommentList.getHotCmtList().get(i).getUid())) {
                                            newsCommentList.getHotCmtList().remove(newsCommentList.getHotCmtList().get(i));
                                            i--;
                                        }
                                    }
                                }
                                if (newsCommentList.getNormalCmtList() != null && newsCommentList.getNormalCmtList().size() > 0) {
                                    for (int i = 0; i < newsCommentList.getNormalCmtList().size(); i++) {
                                        if (InteractiveUtils.getInstance().theDefriendIsContains(newsCommentList.getNormalCmtList().get(i).getUid())) {
                                            newsCommentList.getNormalCmtList().remove(newsCommentList.getNormalCmtList().get(i));
                                            i--;
                                        }
                                    }
                                }


                                if ((newsCommentList.getHotCmtList() != null && newsCommentList.getHotCmtList().size() > 0)
                                        || (newsCommentList.getNormalCmtList() != null && newsCommentList.getNormalCmtList().size() > 0)) {
                                    if (dataList == null) {
                                        dataList = new ArrayList<NormalComment>();
                                    }
                                    if (mAdapter == null) {
                                        mAdapter = new NormalCommentAdapter(dataList, mActivity);
                                        mListView.setAdapter(mAdapter);
                                    }
                                    if (addCommentIndex == 0) {
                                        //先判断热门,因为现在可能只给热门,不给normal
                                        if (newsCommentList.getHotCmtList() != null && newsCommentList.getHotCmtList().size() > 0) {
                                            addCommentIndex = newsCommentList.getHotCmtList().size() + 2;
                                            ((NormalCommentAdapter) mAdapter).currentLastHotIndex = newsCommentList.getHotCmtList().size();
                                        } else {
                                            addCommentIndex = 1;
                                        }
                                        list = newsCommentList.getAllCommentList();
                                    } else {
                                        list = newsCommentList.getNormalCmtList();
                                    }
                                    if (newsCommentList.getNormalCmtList() != null) {
                                        if (newsCommentList.getNormalCmtList().size() <= 0) {
                                            hasNormal = true;
                                        } else {
                                            hasNormal = false;
                                        }
                                    }

                                    if (isRefresh) {
                                        //清空数据
                                        dataList.clear();
                                    }
                                    dataList.addAll(list);
                                    if (mAdapter == null) {
                                        mAdapter = new NormalCommentAdapter(dataList, mActivity);
                                        mListView.setAdapter(mAdapter);
                                    } else {
                                        mAdapter.notifyDataSetChanged();
                                    }
                                    setLoadView(LOADSUCES, null);
                                } else {
                                    if (isRefresh) {
                                        if (dataList == null) {
                                            dataList = new ArrayList<NormalComment>();
                                        }
                                        if (mAdapter == null) {
                                            mAdapter = new NormalCommentAdapter(dataList, mActivity);
                                            mListView.setAdapter(mAdapter);
                                        } else {
                                            mAdapter.notifyDataSetChanged();
                                        }
                                        if (dataList.size() == 0) {
                                            setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_CLOCK, getResources().getString(R.string.no_comment));
                                        }
                                    }
                                    if (dataList != null && dataList.size() > 0) {
                                        setLoadView(LOADSUCES, getResources().getString(R.string.nomore_comment));
                                    }
                                }

                            } else {

                                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
                            }
                        }
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Config.ResultCode.REQUESTCODE_1
                && resultCode == RESULT_OK && data != null) {
            if (mAdapter != null) {
                NormalComment curentCommentEntity = ((NormalCommentAdapter) mAdapter).getCurentCommentEntity();
                if (curentCommentEntity != null) {
                    if (data.getBooleanExtra(Config.Extra.PARAMETER1, false)) {
                        curentCommentEntity.setPraiseNum((StringUtils.toInt(curentCommentEntity.getPraiseNum(), 0) + 1) + "");
                    }
                }

                mAdapter.notifyDataSetChanged();
            }
        }


    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        if (dataList != null) {
//            dataList.clear();
            addCommentIndex = 0;
        }
        super.onPullDownToRefresh(refreshView);

    }


}
